package com.fjnldx.mes.service.Quota;

import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.domainRes.ResultObject;
import com.fjnldx.mes.domain.pojo.MesDinger;
import com.fjnldx.mes.mapper.Quota.QuotaMapper;
import com.fjnldx.mes.utils.Common;
import com.fjnldx.mes.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class QuotaService {
    @Resource
    QuotaMapper quotaMapper;
    @Resource
    Common common;

    /**
     * 定额查询
     */
    public Result queryQuota(MesDinger data) throws Exception {
        try {
            List<MesDinger> list = quotaMapper.queryQuota(data);
            int total = quotaMapper.queryTotalQuota(data);
            if (total > 0) {
                ResultObject<MesDinger> res = new ResultObject<>();
                res.setDataList(list);
                res.setTotal(total);
                return Result.success(res);
            } else {
                return Result.success(null);
            }
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 删除定额
     */
    public Result deleteQuota(String[] ids) throws Exception {
        try {
            int i = quotaMapper.deleteQuota(ids);
            if (i > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("无该定额id");
            }
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 更新定额
     */
    public Result updateQuota(MesDinger data) throws Exception {
        try {
            int i = quotaMapper.updateQuota(data);
            if (i > 0) {
                return Result.success("更新成功");
            } else {
                return Result.error("无该定额id");
            }
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 新增定额
     */
    public Result insertQuota(MesDinger data) throws Exception {
        try {
            MesDinger query = new MesDinger();
            query.setGongxu(data.getGongxu());
            query.setPz(data.getPz());
            int total = quotaMapper.queryTotalQuota(query);
            if (total > 0) {
                return Result.error("该品种已存在该定额记录");
            } else {
                quotaMapper.insertQuota(data);
                return Result.success("新增成功");
            }
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 导出定额表Excel
     *
     * @param request
     * @param response
     * @throws Exception
     */
    public void exportDingErData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MesDinger query = new MesDinger();
        query.setSize(-1);
        //获取数据
        List<MesDinger> list = quotaMapper.queryQuota(query);

        //excel标题
        String[] title = {"工序", "品种", "锭速", "锭量", "效率", "车速", "重量", "台产", "计算"};

        //excel文件名
        String fileName = "定额表数据" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "定额表";

        String[][] content = new String[list.size()][title.length];
        for (int i = 0; i < list.size(); i++) {
            MesDinger obj = list.get(i);
            content[i][0] = obj.getGongxu() == null ? "" : obj.getGongxu();
            content[i][1] = obj.getPz() == null ? "" : obj.getPz();
            content[i][2] = obj.getDs() == null ? "" : obj.getDs();
            content[i][3] = obj.getDl() == null ? "" : obj.getDl();
            content[i][4] = obj.getXiaolv() == null ? "" : obj.getXiaolv();
            content[i][5] = obj.getCs() == null ? "" : obj.getCs();
            content[i][6] = obj.getZl() == null ? "" : obj.getZl();
            content[i][7] = obj.getTc() == null ? "" : obj.getTc();
            content[i][7] = obj.getJisuan() == null ? "" : obj.getJisuan();
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            common.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            throw new Exception();
        }
    }
    /**
     * 导出定额表Excel模板
     * @param request
     * @param response
     * @throws Exception
     */
    public void exportDingErEmpty(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //excel标题
        String[] title = {"工序", "品种", "锭速", "锭量", "效率", "车速", "重量", "台产", "计算"};
        //excel文件名
        String fileName = "定额表模板" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "定额表";
        String[][] content = new String[1][title.length];
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            common.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
